cmake_minimum_required(VERSION 3.28)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# 需要与cef编译的版本相对应
if (MSVC)
    set(CMAKE_CXX_FLAGS_DEBUG "/MTd")
    set(CMAKE_CXX_FLAGS_RELEASE "/MT")
endif()

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Debug)
endif()
project(QtCefWindows)
set(CEF_STANDARD_LIBS
    user32.lib
    gdi32.lib
    kernel32.lib
    advapi32.lib
    winmm.lib
    shlwapi.lib
)

add_definitions(-D_ITERATOR_DEBUG_LEVEL=0)

set(CEF_INCLUDE "${CMAKE_SOURCE_DIR}/3rd/cef")
set(CEF_RES "${CMAKE_SOURCE_DIR}/lib/cef/resource")
set(PROJECT_RES "${CMAKE_SOURCE_DIR}/res")

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CEF_LIB "${CMAKE_SOURCE_DIR}/lib/cef/debug")
    set(PROJECT_OUT_DIR "${CMAKE_BINARY_DIR}/Debug")
    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDebug")
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
    set(CEF_LIB "${CMAKE_SOURCE_DIR}/lib/cef/release")
    set(PROJECT_OUT_DIR "${CMAKE_BINARY_DIR}/Release")
    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded")
endif()
include_directories(${CEF_INCLUDE})
set(QTCEFAPP_SRCS
    src/cefapp/windowdelegate.h
    src/cefapp/windowdelegate.cc
    src/cefapp/httpschemefactory.cc
    src/cefapp/httpschemefactory.h
    src/cefapp/mycefapp.cc
    src/cefapp/mycefapp.h
    src/main.cpp
)
source_group(QtCefApp FILES ${QTCEFAPP_SRCS})
set(CEF_TARGET "QtCefApp")
link_directories(${CEF_LIB})
add_executable(${CEF_TARGET} WIN32 ${QTCEFAPP_SRCS})
target_link_libraries(${CEF_TARGET} libcef libcef_dll_wrapper)

#拷贝cef依赖的库文件
add_custom_command(TARGET ${CEF_TARGET} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
    ${CEF_LIB} ${PROJECT_OUT_DIR}
)
#拷贝cef依赖的资源文件，如果资源有缺失，比如其中的icudtl.dat缺失，会导致ICU初始化失败，导致崩溃，你把一些使用了cef的软件的该文件删除，会发现该软件也会出现崩溃或者加载不出来
# 具体参考cef官方实例的cmake文件的组织方式
add_custom_command(TARGET ${CEF_TARGET} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
    ${CEF_RES} ${PROJECT_OUT_DIR}
)
# 拷贝项目依赖的前端资源
add_custom_command(TARGET ${CEF_TARGET} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
    ${PROJECT_RES} ${PROJECT_OUT_DIR}
)